<?php

class DateSelect extends PageElement
{
	private $aDateTime;
	private $disabler = false;
	private $yearRangeStart=2000;
	private $yearRangeEnd=2020;

	public function __Construct()
	{
		$this->aDateTime = new DateTime("now");
	}

	public function setDisabler($disabler)
	{
//		echo "setDisabler(".$disabler.")<br>";
		$this->disabler = $disabler;
//		echo "set as ".$this->disabler."<br>";
	}

	public function setDate($datetime)
	{
		$this->aDateTime = new DateTime($datetime);
	}

	public function setYearRangeStart($aYear)
	{
		$this->yearRangeStart=$aYear;
	}

	public function setYearRangeEnd($aYear)
	{
		$this->yearRangeEnd=$aYear;
	}
	
	public function getYearRangeStart()
	{
		return $this->yearRangeStart;
	}

	public function getYearRangeEnd()
	{
		return $this->yearRangeEnd;
	}

	public function getHTML()
	{

	$container = new Hidden();
	$container->setName($this->name);
	$container->setValue($this->aDateTime->format("Y-m-d"));
	$html = $container->getHTML();

	$html .= "<SCRIPT language=\"javascript\">\n";
	$html .= "function ".$this->name."update()\n";
	$html .= "{\n";
//	$html .= "alert('".$this->name."update()');\n";
	$html .= "document.getElementById(\"".$this->name."\").value = ";
	$html .= "document.getElementById(\"".$this->name."Year"."\").value + ";
	$html .= "\"-\" + ";
	$html .= "document.getElementById(\"".$this->name."Month"."\").value + ";
	$html .= "\"-\" + ";
	$html .= "document.getElementById(\"".$this->name."Day"."\").value ;\n";
	$html .= "if(document.getElementById('".$this->name."Disabler')!= null)\n";
	$html .= "{\n";
	$html .= "if(document.getElementById('".$this->name."Disabler').checked == 1)\n";
	$html .= "{\n";
	$html .= "document.getElementById(\"".$this->name."\").value = '0001-01-01';\n";
	$html .= "}\n";
	$html .= "}\n";
//	$html .= "alert(document.getElementById(\"".$this->name."\").value);\n";
	$html .= "}\n";
	$html .= "</SCRIPT>\n";


	$disabled = "";
	if($this->disabled)
			$disabled .= " disabled=\"disabled\"";
			
	$visibilityText="";
	$noDateSet=false;
	if($this->aDateTime->format('Y-m-d')=='0001-01-01')
	{
		$noDateSet = true;
		$visibilityText="style=\"visibility:hidden;\" ";
	}		

		// day
	$html .= "<select onchange='".$this->name."update()' name='".$this->name."Day' id='".$this->name."Day'".$disabled.$visibilityText.">\n";
	for ($i=1; $i<32;$i++)
		{
		if(strlen($i)==1){$i = "0".$i;}

		$html .= "<option value='$i'";
		if(ltrim($i,"0")==$this->aDateTime->format('d')){$html .= " selected ".$this->aDateTime->format('d');}
		$html .= ">$i</option>\n";
		}
	$html .= "</select> / ";

	// month
	$html .= "<select onchange='".$this->name."update()' name='".$this->name."Month' id='".$this->name."Month'".$disabled.$visibilityText.">\n";
	for ($i=1; $i<13;$i++)
		{
		if(strlen($i)==1){$i = "0".$i;}
		
		$html .= "<option value='$i'";
		if(ltrim($i,"0")==$this->aDateTime->format('m')){$html .= " selected ".$this->aDateTime->format('m');}
		$html .= ">$i</option>\n";
		}
	$html .= "</select> / ";

	// year
	$html .= "<select onchange='".$this->name."update()' name='".$this->name."Year' id='".$this->name."Year'".$disabled.$visibilityText.">\n";
	for ($i=$this->yearRangeStart; $i<$this->yearRangeEnd;$i++)
		{
		$html .= "<option value='$i'";
		if(ltrim($i,"0")==$this->aDateTime->format('Y')){$html .= " selected ".$this->aDateTime->format('Y');}
		$html .= ">$i</option>\n";
		}
	$html .= "</select>";

//	echo "disabler = ".$this->disabler."<br>";
	if($this->disabler)
	{
		$checkbox = new Checkbox();
		$checkbox->setName($this->name."Disabler");
		$checkbox->setText("None");
		$checkbox->setOnClick($this->name.'DisablerClicked();');
		if($this->disabled)
			$checkbox->setDisabled(true);

		if($noDateSet==true)
			$checkbox->setChecked(true);
		
		$html .= "<script language = \"javascript\">\n";
		$html .= "function ".$this->name."DisablerClicked()\n";
		$html .= "{\n";
//		$html .= "alert(document.getElementById('".$this->name."Disabler').checked)\n";
		$html .= "if(document.getElementById('".$this->name."Disabler').checked == true)\n";
		$html .= "{\n";
		$html .= "document.getElementById(\"".$this->name."Year"."\").style.visibility = 'hidden' ;\n";
		$html .= "document.getElementById(\"".$this->name."Month"."\").style.visibility = 'hidden' ;\n";
		$html .= "document.getElementById(\"".$this->name."Day"."\").style.visibility = 'hidden' ;\n";
		$html .= "}\n";
		$html .= "else\n";
		$html .= "{\n";
		$html .= "document.getElementById(\"".$this->name."Year"."\").style.visibility = 'visible' ;\n";
		$html .= "document.getElementById(\"".$this->name."Month"."\").style.visibility = 'visible' ;\n";
		$html .= "document.getElementById(\"".$this->name."Day"."\").style.visibility = 'visible' ;\n";
		$html .= "}\n";
		$html .= $this->name."update();\n";
		$html .= "}\n";
		$html .= "</script>\n";
		
		$html .= $checkbox->getHTML();
	}


	return $html;
	}
}

?>